﻿using AWSV2.Services;
using Common.HardwareSDKS.FFmpeg;
using Common.HardwareSDKS.HIKVision;
using Common.HardwareSDKS.Models;
using Common.Models;
using Common.Utility;
using Common.Utility.AJ;
using FFmpeg.AutoGen;
using IWshRuntimeLibrary;
using LiveChartsCore;
using LiveChartsCore.SkiaSharpView;
using log4net;
using Masuit.Tools.Security;
using MaterialDesignThemes.Wpf;
using NPOI.SS.Formula.Functions;
using Quartz.Impl;
using Quartz;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using Yitter.IdGenerator;
using static Common.HardwareSDKS.HIKVision.CHCNetSDK;
using Common.Model;
using Common.Utility.AJ.Extension;
using System.Diagnostics;
using Common;

namespace AWSV2
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            var setting = SettingsHelper.AWSV2Settings;

            var configLan = setting.Settings["Language"].TryGetString("zh-CN");
            var curLan = Thread.CurrentThread.CurrentCulture.Name;

            if (!string.IsNullOrWhiteSpace(configLan) && !configLan.Equals(curLan))
            {
                I18NService.ChangeLanguage(configLan);
            }

            this.DispatcherUnhandledException += App_DispatcherUnhandledException;
            TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

            #region 检测多开

            var ajSystemParamsCfg = AJUtil.TryGetJSONObject<AJSystemParamsCfg>(setting.Settings[nameof(AJSystemParamsCfg)].TryGetString()) ?? new AJSystemParamsCfg();
            if (!ajSystemParamsCfg.MultiProcess)
            {
                var currentProcess = Process.GetCurrentProcess();

                if (Process.GetProcessesByName(currentProcess.ProcessName)?.Length > 1)
                {
                    MessageBox.Show("程序已经运行", "提示", MessageBoxButton.OK);
                    currentProcess.Kill();
                    return;
                }
            }

            #endregion

            base.OnStartup(e);

            

        }

        protected override void OnExit(ExitEventArgs e)
        {
            AJDatabaseService.Dispose();
            base.OnExit(e);
        }




        


        private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            e.Handled = true;
            if (e.Exception is FriendlyException fex)
            {
                if (!fex.Silent)
                {
                    MessageBox.Show(fex.Message, fex.Title, MessageBoxButton.OK);
                }
                return;
            }
            AJLog4NetLogger.Instance().Error($"{nameof(App_DispatcherUnhandledException)} 全局异常已捕获", e.Exception);

        }

        private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
        {
            e.SetObserved();
            if (e.Exception.GetBaseException() is FriendlyException fex)
            {
                if (!fex.Silent)
                {
                    MessageBox.Show(fex.Message, fex.Title, MessageBoxButton.OK);
                }
                return;
            }
            AJLog4NetLogger.Instance().Error($"{nameof(TaskScheduler_UnobservedTaskException)} 全局异常已捕获", e.Exception);

        }

        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            if (e.IsTerminating)
            {
                if ((e.ExceptionObject as Exception) is FriendlyException fex)
                {
                    if (!fex.Silent)
                    {
                        MessageBox.Show(fex.Message, fex.Title, MessageBoxButton.OK);
                    }
                    return;
                }

                AJLog4NetLogger.Instance().Error($"{nameof(CurrentDomain_UnhandledException)} 全局异常已捕获", e.ExceptionObject as Exception);
            }

        }
    }
}
